package com.mall.service.user;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.user.UserLevelService;
import com.mall.custom.dao.read.UserLevelReadMapper;
import com.mall.custom.dao.write.UserLevelWriteMapper;
import com.mall.pojo.UserLevel;
import com.mall.pojo.UserLevelExample;
import com.mall.pojo.UserLevelExample.Criteria;
@Service
public class UserLevelServiceImpl implements UserLevelService {
	@Autowired
	private UserLevelWriteMapper userLevelWriteMapper;
	@Autowired
	private UserLevelReadMapper userLevelReadMapper;

	@Override
	public int insertUserLevel(UserLevel userLevel) {
		userLevel.setGmtCreate(new Date());
		userLevel.setGmtModify(new Date());
		return this.userLevelWriteMapper.insert(userLevel);
	}

	@Override
	public UserLevel getUserLevelByPrice(Long sumPrice,Integer commOnlyKey) {
		UserLevelExample example = new UserLevelExample();
		Criteria criteria = example.createCriteria();
		criteria.andCommOnlyKeyEqualTo(commOnlyKey);
		criteria.andStatusEqualTo(0);//有效
		
		List<UserLevel> userLevelList = this.userLevelReadMapper.selectByExample(example);
		if (userLevelList == null || userLevelList.size()<=0) {
			return null;
		}
		for (UserLevel userLevel : userLevelList) {
			if (sumPrice.longValue()>= userLevel.getLowPrice().longValue() && sumPrice.longValue()<= userLevel.getHighPrice().longValue()) {
				return userLevel;
			}
		}
		
		return userLevelList.get(userLevelList.size());
	}

	@Override
	public UserLevel getUserLevelByLevelAndKey(Integer level, Integer commOnlyKey) {
		UserLevelExample example = new UserLevelExample();
		Criteria criteria = example.createCriteria();
		if(level == null || commOnlyKey == null){
			return null;
		}
		criteria.andLevelEqualTo(level);
		criteria.andCommOnlyKeyEqualTo(commOnlyKey);
		criteria.andStatusEqualTo(0);//有效
		List<UserLevel> userLevelList = this.userLevelReadMapper.selectByExample(example);
		if (userLevelList == null || userLevelList.size()<=0) {
			return null;
		}
		return userLevelList.get(0);
	}

	@Override
	public List<UserLevel> getUserLevelListByKey(Integer commOnlyKey) {
		if(commOnlyKey == null){
			return null;
		}
		UserLevelExample example = new UserLevelExample();
		example.setOrderByClause("level asc");
		Criteria criteria = example.createCriteria();
		criteria.andCommOnlyKeyEqualTo(commOnlyKey);
		criteria.andStatusEqualTo(0);
		return this.userLevelReadMapper.selectByExample(example);
	}

	@Override
	public int modifyUserLevel(UserLevel userLevel) {
		userLevel.setGmtModify(new Date());
		return this.userLevelWriteMapper.updateByPrimaryKey(userLevel);
	}

	@Override
	public UserLevel getUserLevelById(Long userLevelId) {
		if(userLevelId == null || userLevelId <= 0){
			return null;
		}
		return this.userLevelReadMapper.selectByPrimaryKey(userLevelId);
	}

	@Override
	public int replaceUserLevelList(List<UserLevel> userLevelList) {
		if(userLevelList == null || userLevelList.size() <= 0){
			return 0;
		}
		return this.userLevelWriteMapper.replaceUserLevel(userLevelList);
	}

	

}
